package com.baselibrary.manager;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatDialog;

import com.baselibrary.R;
import com.baselibrary.utils.CommonUtil;
import com.baselibrary.utils.ContextUtils;
import com.baselibrary.utils.OnClickListener;

/**
 * Created by Administrator on 2017/9/15.
 */

public class DialogManager {

    public static AppCompatDialog createLoadingDialog(Activity act) {
        return createLoadingDialog(act, "");
    }

    @SuppressLint("SetTextI18n")
    public static AppCompatDialog createLoadingDialog(Activity act, String content) {
        if (null != act) {
            AppCompatDialog appCompatDialog = new AppCompatDialog(act, R.style.LoadingDialogTheme);
            @SuppressLint("InflateParams")
            View view = act.getLayoutInflater().inflate(R.layout.web_layout_loading, null);
            appCompatDialog.setContentView(view);
            TextView title = view.findViewById(R.id.content);
            ImageView progress = view.findViewById(R.id.progress);
            if (!CommonUtil.isBlank(content)) {
                title.setText("" + content);
            }
            Animation antv = AnimationUtils.loadAnimation(act, R.anim.loading_progressbar);
            LinearInterpolator lin = new LinearInterpolator();
            antv.setInterpolator(lin);
            antv.setRepeatCount(-1);
            progress.startAnimation(antv);
            appCompatDialog.setCanceledOnTouchOutside(false);
            return appCompatDialog;
        } else {
            return null;
        }
    }
    /**
     * 贴心提示
     *
     * @param act
     */
    public static void showTipsDialog(Activity act) {
        final AlertDialog dialog = new AlertDialog.Builder(act, AlertDialog.THEME_HOLO_DARK).create();
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.getDecorView().setBackgroundColor(act.getResources().getColor(R.color.transparent));
        window.setAttributes(lp);
        window.setContentView(R.layout.release_tip);
        window.setGravity(Gravity.CENTER);
        window.findViewById(R.id.tv_release_specific).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    }
    public static void showSavePictureDialog(Activity act, boolean isVideo, final OnClickListener listener) {
        final AlertDialog dialog = new AlertDialog.Builder(act, AlertDialog.THEME_HOLO_DARK).create();
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.getDecorView().setBackgroundColor(act.getResources().getColor(R.color.transparent));
        window.setAttributes(lp);
        window.setContentView(R.layout.dialog_save_picture);
        window.setGravity(Gravity.BOTTOM);

        TextView tvSavePicture = window.findViewById(R.id.tv_save_picture);
        TextView mTvCancel = window.findViewById(R.id.tv_release_cancel);

        if (isVideo) {
            tvSavePicture.setText("保存视频");
        }

        tvSavePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v, 2);
                }

                dialog.cancel();
            }
        });
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

    }
    private void initView() {

    }

    public interface Listener {
        void onItemLeft();

        void onItemRight();
    }
    /**
     * 改版后的 相机相册
     *
     * @param act
     * @param listener
     */
    public static void showDynamicDialog(Activity act, final OnClickListener listener) {
        final AlertDialog dialog = new AlertDialog.Builder(act, AlertDialog.THEME_HOLO_DARK).create();
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.getDecorView().setBackgroundColor(act.getResources().getColor(R.color.transparent));
        window.setAttributes(lp);
        window.setContentView(R.layout.release_selectmodel);
        window.setGravity(Gravity.BOTTOM);

        TextView mTvCamera = window.findViewById(R.id.tv_release_camera);
        TextView mTvAlbum = window.findViewById(R.id.tv_release_album);
        TextView mTvCancel = window.findViewById(R.id.tv_release_cancel);

        mTvCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (listener != null) {
                    listener.onClick(v, 1);
                }

                dialog.cancel();

            }
        });


        mTvAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v, 2);
                }

                dialog.cancel();
            }
        });
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

    }
    public static void showConfirmDialog(Activity act, String content, final Listener listener) {
        showConfirmDialog(act, null, content, null, null, listener);
    }
    public static void showConfirmDialog(Activity act, String title, String content, String leftText, String rightText, final Listener listener) {
        _showConfirmDialog(act, title, content, leftText, rightText, listener, false);
    }

    public static AlertDialog _showConfirmDialog(Activity act, String title, String content, String leftText, String rightText, final Listener listener, boolean hideLeft) {
        final AlertDialog dialog = new AlertDialog.Builder(act, AlertDialog.THEME_HOLO_DARK).create();
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.getDecorView().setBackgroundColor(act.getResources().getColor(R.color.transparent));
        window.setContentView(R.layout.view_confirm_dialog_alert);
        window.setAttributes(lp);
        TextView tvTitle = window.findViewById(R.id.title);
        TextView tvContent = window.findViewById(R.id.content);
        TextView tvLeft = window.findViewById(R.id.tv_left);
        TextView tvRight = window.findViewById(R.id.tv_right);
        View vCenterLine = window.findViewById(R.id.v_center_line);
        tvContent.setText(content);
        if (!CommonUtil.isBlank(title)) {
            tvTitle.setText(title);
        }
        if (!CommonUtil.isBlank(leftText)) {
            tvLeft.setText(leftText);
        }
        if (hideLeft) {
            tvLeft.setVisibility(View.GONE);
            vCenterLine.setVisibility(View.GONE);
        }
        if (!CommonUtil.isBlank(rightText)) {
            tvRight.setText(rightText);
        }
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemLeft();
                }
                dialog.cancel();
            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemRight();
                }
                dialog.cancel();
            }
        });
        return dialog;
    }
    @SuppressLint("SetTextI18n")
    public static void showCoinDialog(final Activity act, final String title, final int coin) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    Class mClass = Class.forName("com.hanxiang.activity.CoinActivity");
                    Intent intent = new Intent(ContextUtils.getCurApplication(), mClass);
                    intent.putExtra("title", title);
                    intent.putExtra("coin", coin);
                    act.startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }, 1000);
    }

    public interface ICallBack {
        //用户协议
        void onUserClick();

        //隐私
        void onPrivacyClick();

        void onOkClick();

        void onNoClick();
    }

    public static AlertDialog showConfirmDialog_hideLeft(Activity act, String title, String content, final Listener listener) {
        return _showConfirmDialog(act, title, content, null, null, listener, true);
    }


    public static void showRoomCodeDialog(Activity act, final OnClickListener listener, final OnClickListener leftListener) {
        final AlertDialog dialog = new AlertDialog.Builder(act, AlertDialog.THEME_HOLO_DARK).create();
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.getDecorView().setBackgroundColor(act.getResources().getColor(R.color.transparent));
        window.setContentView(R.layout.view_roomcode_dialog_alert);
        final EditText content = window.findViewById(R.id.content);
        final ImageView deleteView = window.findViewById(R.id.deleteView);
        TextView tvLeft = window.findViewById(R.id.tv_left);
        TextView tvRight = window.findViewById(R.id.tv_right);
        deleteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content.setText("");
                deleteView.setVisibility(View.GONE);
            }
        });
        content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                if (charSequence != null) {
                    deleteView.setVisibility(charSequence.length() > 0 ? View.VISIBLE : View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftListener != null) {
                    leftListener.onClick(v, content.getText().toString().trim());
                }
                dialog.cancel();
            }
        });

        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v, content.getText().toString().trim());
                }
                dialog.cancel();
            }
        });
    }

}
